//
//  AdViewController.m
//  Budejie
//
//  Created by 远成物流 on 2017/5/12.
//  Copyright © 2017年 陈梦佳. All rights reserved.
//

#import "AdViewController.h"
#import <AFNetworking.h>
#import "AdModel.h"
#import <MJExtension/MJExtension.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "TabBarController.h"

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface AdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adContainerView;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property (strong, nonatomic) UIImageView *adView;
@property (strong, nonatomic) AdModel *adModel;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation AdViewController

-(UIImageView *)adView{
    if (_adView == nil) {
        
        _adView = Alloc(UIImageView);
        _adView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        
        [_adView addGestureRecognizer:tapGesture];
        
        [self.adContainerView addSubview:_adView];
    }
    
    return _adView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.加载启动图片
    [self loadLaunchImage];
    
    //2.加载广告数据
    [self loadAdData];
    
    //倒计时3秒
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange:) userInfo:nil repeats:YES];
}

//跳过按钮点击
- (IBAction)jumpBtnAction:(id)sender {
    TabBarController *tabbar = [[TabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
    
    [_timer invalidate];
}

//广告图片点击
-(void)tapAction:(UITapGestureRecognizer *)gesture{
    //->safari
    NSURL *url = [NSURL URLWithString:_adModel.ori_curl];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url options:nil completionHandler:nil];
    }
}

#pragma mark 

-(void)loadLaunchImage{
    if (iphone6P) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (iphone6){
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h@2x"];
    }else if (iphone5){
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700-568h@2x"];
    }else{
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage"];
    }
}

-(void)loadAdData{
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",@"text/html",@"text/json",@"text/javascript",@"text/xml",@"image/*"]];
    //参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = code2;
    
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *adDict = [responseObject[@"ad"] lastObject];
        _adModel = [AdModel mj_objectWithKeyValues:adDict];
        //创建UIImageView展示图片
        CGFloat h = ScreenW/_adModel.w*_adModel.h;
        self.adView.frame = CGRectMake(0, 0, ScreenW, h);
        [self.adView sd_setImageWithURL:[NSURL URLWithString:_adModel.w_picurl]];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Log(@"%@",error.localizedDescription);
    }];
}

-(void)timeChange:(id)sender{
    static int i=3;
    
    if (i==0) {
        [self jumpBtnAction:nil];
    }
    
    i--;
    
    [_jumpBtn setTitle:[NSString stringWithFormat:@"跳过(%d)",i] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
